package utils

import (
	"encoding/json"
	"runtime"
)

// https://blog.csdn.net/subfate/article/details/123851931
type SysInfo struct {
	OS       string `json:"os"`
	Arch     string `json:"arch"`
	CpuCores int    `json:"cpu_cores"`
}

func (b *SysInfo) FromJson(payload string) error {
	if err := json.Unmarshal([]byte(payload), b); err != nil {
		return err
	}
	return nil
}

func (b *SysInfo) FromJsonBytes(data []byte) error {
	if err := json.Unmarshal(data, b); err != nil {
		return err
	}
	return nil
}

func (b *SysInfo) ToJson() (string, error) {
	data, err := json.Marshal(b)
	if err != nil {
		return "", err
	}
	return string(data), nil
}

func GetSysInfo() *SysInfo {
	var info SysInfo
	info.OS = runtime.GOOS
	info.Arch = runtime.GOARCH
	info.CpuCores = runtime.GOMAXPROCS(0)
	return &info
}
